package client_controllers;


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import client_gui.DefineCategoryGUI;
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.Boss;


/**
 * @class DefineCategoryController ,in this class the marketing manager define new category.
 * 
 * @param DefineCategoryGUI links to GUI.
 * @param EmployeeLoginController links to employee controller. 
 * @param Boss is used as an instance, to send objects to DB and allow changes in the lists.
 * @param MarketingEmployeeController links to marketing employee controller.
 */
public class DefineCategoryController implements dbHandler {
	
	private DefineCategoryGUI dcg;
	private MarketingManagerController mmc;
	private EmployeeLoginController elc;
	private Boss b;
	private ArrayList <String> categorys;
	
	
	public DefineCategoryController(DefineCategoryGUI dcg,
			MarketingManagerController mmc, EmployeeLoginController elc) {
		this.dcg=dcg;
		this.mmc=mmc;
		this.elc=elc;
		this.dcg.getBtnBack().addActionListener(new Back());
		this.dcg.getBtnSubmit().addActionListener(new Submit());
		//this.dcg.addWindowListener(new exit());
		
		getCategoryCombo();
	}
	
	/**
	 * This function getCategory() get category combo to show the category that already exists
	 */
	private void getCategoryCombo() {
		this.setB(new Boss(null,"getcategorycombo","DefineCategoryController"));
		this.Search();
		
	}

	public class Back implements ActionListener{
		public void actionPerformed(ActionEvent e) {
		getDcg().dispose();
		getMmc().getMmg().setVisible(true);		
		}
	}
	
	/**
	 * @class Submit implements ActionListener,save the new category in DB
	 * 
	 */
	public class Submit implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			String newcat=getDcg().getTextField().getText();
			for(String i :categorys )
				if(i.compareTo(newcat)==0){
					dcg.getLblErr().setForeground(Color.red);
					dcg.getLblErr().setVisible(true);
					return;
				}
			b.setCommand("addCategoryToTable");
			b.setControl("DefineCategoryController");
			b.setObj(newcat);
			Add();
		}
	}
	



	
	
	@Override
	public void Add() {
		
		try{CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
			
		}catch (IOException ex){
			ex.printStackTrace();
		}
		
		
	}
	
	

	public DefineCategoryGUI getDcg() {
		return dcg;
	}

	public void setDcg(DefineCategoryGUI dcg) {
		this.dcg = dcg;
	}

	public MarketingManagerController getMmc() {
		return mmc;
	}

	public void setMmc(MarketingManagerController mmc) {
		this.mmc = mmc;
	}

	public EmployeeLoginController getElc() {
		return elc;
	}

	public void setElc(EmployeeLoginController elc) {
		this.elc = elc;
	}

	public Boss getB() {
		return b;
	}

	public void setB(Boss b) {
		this.b = b;
	}

	public ArrayList<String> getCategorys() {
		return categorys;
	}

	public void setCategorys(ArrayList<String> categorys) {
		this.categorys = categorys;
	}

	


	@Override
	public void Search() {
		// TODO Auto-generated method stub
		try{CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
			
		}catch (IOException ex){
			ex.printStackTrace();
		}
		
	}

	@Override
	public String Delete() {
		// TODO Auto-generated method stub
		return null;
	}

	
	/**
	 * the function HandleMsg get massage from server
	 * @param msg it item from the DB Usually comes as Boss
	 */
	@Override
	public void HandleMsg(Object msg) {
		// TODO Auto-generated method stub
		this.b=(Boss) msg;
		
		if(this.getB().getCommand().equals("getcategorycombo")){
			this.dcg.getCategoryCombo().removeAll();///check this
			categorys=(ArrayList<String>) this.getB().getObj();
			for(String i :categorys )
				dcg.getCategoryCombo().addItem(i);
		}
		
		else if(this.getB().getMsg().equals("Category successfully addad")){
				this.dcg.getLblErr().setText("Category successfully addad");
				this.dcg.getLblErr().setForeground(Color.blue);
				this.dcg.getLblErr().setVisible(true);	
				
				dcg.getCategoryCombo().removeAllItems();
				getCategoryCombo();
			}
		else if(this.getB().getMsg().equals("Category already exist")){
			this.dcg.getLblErr().setText("Category already exist");
			this.dcg.getLblErr().setForeground(Color.red);
			this.dcg.getLblErr().setVisible(true);	
		}
		
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}
	
	

}
